/**
 * 
 */
package com.thinkgsp.jxc.model;

import java.io.Serializable;
import java.math.BigDecimal;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import com.thinkgsp.util.XlsFieldAnnotation;
import com.thinkgsp.util.XlsValueType;

/**
 * 商品存货类
 * @author evan
 *
 */
public class Inventory extends BaseModel implements Serializable {

	private static final long serialVersionUID = 4691547251604455644L;

	private Long id;
	private Integer invAddId;
	private Integer productId;
	private Long batchNoId;
	@XlsFieldAnnotation(ChsName="产品",Width="50",RefType= XlsValueType.RefClass,MethodName="getProduct")
	private Product product;
	@XlsFieldAnnotation(ChsName="年份",Width="10",MethodName="getFyear")
	private Integer fyear;
	@XlsFieldAnnotation(ChsName="期间",Width="10",MethodName="getPeriod")
	private Integer period;
	@XlsFieldAnnotation(ChsName="期初数量",MethodName="getBeginQty")
	private BigDecimal beginQty = new BigDecimal(0);
	@XlsFieldAnnotation(ChsName="期初金额",MethodName="getBeginAmt")
	private BigDecimal beginAmt = new BigDecimal(0);
	@XlsFieldAnnotation(ChsName="入库数量",MethodName="getInQty")
	private BigDecimal inQty = new BigDecimal(0);
	@XlsFieldAnnotation(ChsName="入库金额",MethodName="getInAmt")
	private BigDecimal inAmt = new BigDecimal(0);
	@XlsFieldAnnotation(ChsName="出库数量",MethodName="getOutQty")
	private BigDecimal outQty = new BigDecimal(0);
	@XlsFieldAnnotation(ChsName="出库金额",MethodName="getOutAmt")
	private BigDecimal outAmt = new BigDecimal(0);
	@XlsFieldAnnotation(ChsName="成本价",MethodName="getCostPrice")
	private BigDecimal costPrice = new BigDecimal(0);
	private BigDecimal waitInQty = new BigDecimal(0);
	private BigDecimal waitOutQty = new BigDecimal(0);
	private Integer state = 0;
	
	private InventoryAdd inventoryAdd;
	private ProductBatchNo batchNo;
	@XlsFieldAnnotation(ChsName="本期库存数量",MethodName="getUsableQty")
	private BigDecimal usableQty;
	@XlsFieldAnnotation(ChsName="本期库存金额",MethodName="getRemainAmt")
	private BigDecimal remainAmt=BigDecimal.ZERO;
	/**
	 * 存货状态(0 可用;1 不可用)
	 * @return
	 */
	public Integer getState() {
		return state;
	}

	public void setState(Integer state) {
		this.state = state;
	}

	/**
	 * 存货产品对象
	 * @return
	 */
	
	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
		if(product != null) {
			this.productId = product.getProductId();
		}
	}

	/**
	 * 存货库存地址
	 * @return
	 */
	public InventoryAdd getInventoryAdd() {
		return inventoryAdd;
	}

	public void setInventoryAdd(InventoryAdd inventoryAdd) {
		this.inventoryAdd = inventoryAdd;
		if(inventoryAdd != null) {
			this.invAddId = inventoryAdd.getId();
		}
	}

	/**
	 * 存货批号
	 * @return
	 */
	public ProductBatchNo getBatchNo() {
		return batchNo;
	}

	public void setBatchNo(ProductBatchNo batchNo) {
		this.batchNo = batchNo;
		this.batchNoId = batchNo.getId();
	}

	/**
	 * 存货ID
	 * @return
	 */
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * 存货地址ID
	 * @return
	 */
	public Integer getInvAddId() {
		return invAddId;
	}

	public void setInvAddId(Integer invAddId) {
		this.invAddId = invAddId;
	}

	/**
	 * 产品ID
	 * @return
	 */
	public Integer getProductId() {
		return productId;
	}

	public void setProductId(Integer productId) {
		this.productId = productId;
	}

	/**
	 * 批号ID
	 * @return
	 */
	public Long getBatchNoId() {
		return batchNoId;
	}

	public void setBatchNoId(Long batchNoId) {
		this.batchNoId = batchNoId;
	}

	/**
	 * 年份
	 * @return
	 */
	
	public Integer getFyear() {
		return fyear;
	}

	public void setFyear(Integer fyear) {
		this.fyear = fyear;
	}

	/**
	 * 期间
	 * @return
	 */
	public Integer getPeriod() {
		return period;
	}

	public void setPeriod(Integer period) {
		this.period = period;
	}

	/**
	 * 期初数量
	 * @return
	 */
	public BigDecimal getBeginQty() {
		return beginQty;
	}

	public void setBeginQty(BigDecimal beginQty) {
		this.beginQty = beginQty;
	}

	/**
	 * 期初金额
	 * @param beginQty
	 */
	public BigDecimal getBeginAmt() {
		return beginAmt;
	}

	public void setBeginAmt(BigDecimal beginAmt) {
		this.beginAmt = beginAmt;
	}

	/**
	 * 本期入库数量
	 * @return
	 */
	public BigDecimal getInQty() {
		return inQty;
	}

	public void setInQty(BigDecimal inQty) {
		this.inQty = inQty;
	}

	/**
	 * 本期入库金额
	 * @return
	 */
	public BigDecimal getInAmt() {
		return inAmt;
	}

	public void setInAmt(BigDecimal inAmt) {
		this.inAmt = inAmt;
	}

	/**
	 * 本期出库数量
	 * @return
	 */
	public BigDecimal getOutQty() {
		return outQty;
	}

	public void setOutQty(BigDecimal outQty) {
		this.outQty = outQty;
	}

	/**
	 * 本期出库金额
	 * @return
	 */
	public BigDecimal getOutAmt() {
		return outAmt;
	}

	public void setOutAmt(BigDecimal outAmt) {
		this.outAmt = outAmt;
	}

	/**
	 * 成本价
	 * @return
	 */
	public BigDecimal getCostPrice() {
		return costPrice;
	}

	public void setCostPrice(BigDecimal costPrice) {
		this.costPrice = costPrice;
	}

	/**
	 * 等待入库数量
	 * @return
	 */
	public BigDecimal getWaitInQty() {
		return waitInQty;
	}

	public void setWaitInQty(BigDecimal waitInQty) {
		this.waitInQty = waitInQty;
	}

	/**
	 * 等待出库金额
	 * @return
	 */
	public BigDecimal getWaitOutQty() {
		return waitOutQty;
	}

	public void setWaitOutQty(BigDecimal waitOutQty) {
		this.waitOutQty = waitOutQty;
	}
	
	/**
	 * 获取当前库存数量
	 * @return
	 */
	public BigDecimal getRemainQty() {
		BigDecimal remainQty = BigDecimal.ZERO;
		if(beginQty != null) {
			remainQty = remainQty.add(beginQty);
		}
		if(inQty != null) {
			remainQty = remainQty.add(inQty);
		}
		if(outQty != null) {
			remainQty = remainQty.subtract(outQty);
		}
		return remainQty;
	}
	
	
	/**
	 * 获取当前库存金额
	 * @return
	 */
	public BigDecimal getRemainAmt() {
		if(beginAmt != null) {
			remainAmt = remainAmt.add(beginAmt);
		}
		if(inAmt != null) {
			remainAmt = remainAmt.add(inAmt);
		}
		if(outAmt != null) {
			remainAmt = remainAmt.subtract(outAmt);
		}
		return remainAmt;
	}
	
//	/**
//	 * 获取当前可用库存
//	 * 可用库存=实际库存 - 待出库库存
//	 * @return
//	 */
//	public BigDecimal getUsableRemainQty() {
//		BigDecimal usableQty = getRemainQty();
//		if(waitOutQty != null) {
//			usableQty = usableQty.add(waitOutQty);
//		}
//		return usableQty;
//	}
	
	/**
	 * 获取当前可用库存
	 * 当前可用库存= 实际库存-等待出库库存
	 * 当前可用库存不包含等待入库库存
	 * @return 如果可用状态不等于零，则可用库存为零
	 */
	public BigDecimal getUsableQty() {
		if(this.state == 1) {
			return BigDecimal.ZERO;
		} else {
			BigDecimal remainQty = getRemainQty();
			if(waitOutQty != null) {
				remainQty = remainQty.subtract(waitOutQty);
			}
			return remainQty;
		}
	}

	/* (non-Javadoc)
	 * @see com.thinkgsp.jxc.model.BaseModel#toString()
	 */
	@Override
	public String toString() {
		ToStringBuilder sb = new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE);
		sb.append("id",this.id)
		  .append("invAddId",this.invAddId)
		  .append("productId",this.productId);
		
		return sb.toString();
	}

	/* (non-Javadoc)
	 * @see com.thinkgsp.jxc.model.BaseModel#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object o) {
		if(this == o) {
			return true;
		}
		if(!(o instanceof Inventory)) {
			return false;
		}
		final Inventory inv = (Inventory)o;
		if(id != null) {
			return id.equals(inv.getId());
		} else {
			return inv.getId() == null;
		}
	}

	/* (non-Javadoc)
	 * @see com.thinkgsp.jxc.model.BaseModel#hashCode()
	 */
	@Override
	public int hashCode() {
		return (this.id != null ? this.id.hashCode() : 0);
	}

	
}
